package com.yangxiangrice.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangxiangrice.pojo.Result;
import com.yangxiangrice.pojo.SeedList;
import com.yangxiangrice.pojo.UserList;
import com.yangxiangrice.service.SeedListService;
import com.yangxiangrice.service.UserListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @author: shixiaojian
 * @date: 2021年11月17日 19:27
 */
@Controller
@RequestMapping("/seedlist")
@CrossOrigin
@Slf4j
public class SeedListController {
    @Autowired
    SeedListService seedListService;

    @GetMapping(value = "/listpage")
    @ResponseBody
    public Result listpage(@RequestParam("page") int page,
                           @RequestParam("limit") int limit) {
        Result<IPage> result = new Result<>();
        IPage<SeedList> seedListIPage = seedListService.seletePage(new Page<>(page, limit), null);
        return result.ok(seedListIPage);
    }

    /*@PostMapping(value = "/save")
    @ResponseBody
    public Result save(@RequestParam("file") MultipartFile file,
                       @RequestParam Map<String, String> map) throws IOException {
        SeedList seedList = new SeedList();
        Result<Boolean> result = new Result<>();
        if (map.size() > 0) {
            String seedName = map.get("seedName");
            String unitName = map.get("unitName");
            String germRate = map.get("germRate");
            String unitPrice = map.get("unitPrice");
            String origin = map.get("origin");
            seedList.setSeedName(seedName);
            seedList.setUnitName(unitName);
            seedList.setGermRate(new Integer(germRate));
            seedList.setUnitPrice(unitPrice);
            seedList.setOrigin(origin);
            String pics = seedName + ".png";
            seedList.setCreatetime(new Timestamp(System.currentTimeMillis()));
            File dir = new File("E:\\前端\\yangxiang\\yangxiangrice\\src\\upload");
//        if (!dir.exists()) {
//            dir.mkdir();
//        }
            if (!file.isEmpty()) {
                file.transferTo(new File(dir + "\\" + pics));
            }
            seedList.setPics(dir + "\\" + pics);
        }
        boolean tag = seedListService.saveOrUpdate(seedList);
        if (tag) {
            result.ok(tag);
        } else {
            result.fail("保存失败");
        }
        return result;
    }*/

    @PostMapping(value = "/save")
    @ResponseBody
    public Result save(@RequestBody SeedList seedList) {
        Result<Boolean> result = new Result<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("seedName", seedList.getSeedName());
        if(ObjectUtil.isEmpty(seedList.getSeedId())){
            if (seedListService.count(wrapper) > 0) {
                result.fail("种子名称已存在，请重新输入");
            }else{
                seedList.setCreatetime(new Timestamp(System.currentTimeMillis()));
                boolean tag = seedListService.save(seedList);
                if (tag) {
                    result.ok(tag);
                }else {
                    result.fail("添加失败，请重试");
                }
            }
        }else{
            if (seedListService.count(wrapper) > 0) {
                result.fail("种子名称已存在，请重新输入");
            }else{
                seedList.setCreatetime(new Timestamp(System.currentTimeMillis()));
                boolean tag = seedListService.updateById(seedList);
                if (tag) {
                    result.ok(tag);
                }else {
                    result.fail("更新失败，请重试");
                }
            }
        }
        return result;
    }

    @PostMapping(value = "/delete")
    @ResponseBody
    public Result delete(@RequestParam(value = "seedId") Integer userId) {
        Result<Boolean> result = new Result<>();
        boolean tag = seedListService.removeById(userId);
        if (tag) {
            result.ok(tag);
        } else {
            result.fail("删除失败");
        }
        return result;
    }

    @PostMapping(value = "/deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestBody int[] ids) {
        ArrayList<Integer> list = new ArrayList<>();
        Result<Boolean> result = new Result<>();
        for (int id : ids) {
            list.add(id);
        }
        boolean tag = seedListService.removeByIds(list);
        if (tag) {
            result.ok(tag);
        } else {
            result.fail("删除失败");
        }
        return result;
    }

    @RequestMapping(value = "/search")
    @ResponseBody
    public Result search(@RequestParam("input") String input,
                         @RequestParam("page") int page,
                         @RequestParam("limit") int limit) {
        Result<IPage> result = new Result<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("seedName",input);
        IPage<SeedList> seedListIPage = seedListService.seletePage(new Page<>(page, limit), wrapper);
        if (ObjectUtil.isEmpty(seedListIPage)) {
            result.fail("未找到符合条件的数据");
        } else {
            result.ok(seedListIPage);
        }
        return result;
    }
}
